package com.panda.pandawash.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.panda.pandawash.R;

/**
 * Created by gaowf on 2017/3/15.
 */

public class PlateManulInputActivity extends AppCompatActivity {

    // 编辑车牌号@ViewInject(R.id.edit_license_plate)
    private EditText licensePlate;
    private String licensePlateName;

    //下一步按钮
    private Button nextBtn;

    private ImageView backBtn;

    private Intent mIntent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_plate);

        licensePlate = (EditText) findViewById(R.id.edit_license_plate_input);

        nextBtn = (Button) findViewById(R.id.btn_license_plate_next);
        nextBtn.setOnClickListener(mOnClickListener);

        backBtn = (ImageView) findViewById(R.id.btn_return_license);
        backBtn.setOnClickListener(mOnClickListener);


        initRes();

    }

    private void initRes() {
        mIntent = getIntent();
        licensePlateName = mIntent.getStringExtra("scanplatename");
        if(licensePlateName != null){
            licensePlate.setText(licensePlateName);
        }

        licensePlate.addTextChangedListener(watcher);
        licensePlateName = licensePlate.getText().toString().trim();
        if (licensePlateName.length() > 0 ) {
            nextBtn.setBackgroundResource(R.drawable.ic_register_btn);
            nextBtn.setTextColor(0xffffffff);
            nextBtn.setClickable(true);
        } else {
            nextBtn.setBackgroundResource(R.drawable.ic_btn_disable);
            nextBtn.setTextColor(0x80ffffff);
            nextBtn.setClickable(false);
        }
    }
    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {

            /// 车牌号是 5 位,车牌号若为手动输入,可以做成省、市下拉选择框的形式
            if (s.toString().length() ==7) {
                nextBtn.setBackgroundResource(R.drawable.ic_register_btn);
                nextBtn.setClickable(true);
                nextBtn.setTextColor(0xffffffff);
                licensePlateName = s.toString();
            } else {
                nextBtn.setBackgroundResource(R.drawable.ic_btn_disable);;
                nextBtn.setClickable(false);
                nextBtn.setTextColor(0x88ffffff);
            }

        }
    };


    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_license_plate_next:
                    Intent intent = new Intent(PlateManulInputActivity.this, PlateConfirmActivity.class);
                    intent.putExtra("platename",licensePlateName);
                    startActivity(intent);
                    //finish();
                    break;

                case R.id.btn_return_license:
                    finish();
                    break;

            }
        }
    };

}

